//g++ -I. -L.     db.cpp -o db.exe sqlite3.dl
//db > D:\mingw_project\v8test\global_data.js
//change log 2013.3.19.add LAST_TERM FIELD 

#include "db.h"

void initdb(sqlite3 **CURRENT_DB)
{
	int rc = sqlite3_open(DB_LOCATION_HOME, CURRENT_DB);
}

int query(sqlite3 *CURRENT_DB,char * sql,char ***CURRENT_RECORDS,int * CURRENT_ROWS,int* CURRENT_COLS)
{
	
initdb(&CURRENT_DB);
    char *zErrMsg=NULL;
    
     int   rc = sqlite3_get_table
        (
  			CURRENT_DB,              /* An open database */
  			sql,       /* SQL to be executed */
  			CURRENT_RECORDS,       /* Result written to a char *[]  that this points to */
  			CURRENT_ROWS,             /* Number of result rows written here */
  			CURRENT_COLS,          /* Number of result columns written here */
  			&zErrMsg          /* Error msg written here */
  			);
if(zErrMsg!=NULL)
{
  printf("%s",zErrMsg);
  zErrMsg=NULL;
}
sqlite3_close(CURRENT_DB);
        return rc;
}
RECORD* rowsToRecords(char**CURRENT_RECORDS,int CURRENT_ROWS,int CURRENT_COLS )
{
if(CURRENT_RECORDS==NULL)return NULL;
RECORD* result=malloc(CURRENT_ROWS*sizeof(RECORD));
char buf[80]; 
char number[16];
char lastterm[10];	
	for(int i=1;i<=CURRENT_ROWS;i++)//line,注意，recoreds包含了列名字
	   {
	   	snprintf(result[i-1].term,TERMLENGTH,"%s",CURRENT_RECORDS[i*CURRENT_COLS]);
		result[i-1].red1=atoi(CURRENT_RECORDS[i*CURRENT_COLS+1]);//red1
		result[i-1].red2=atoi(CURRENT_RECORDS[i*CURRENT_COLS+2]);//red2
		result[i-1].red3=atoi(CURRENT_RECORDS[i*CURRENT_COLS+3]);//red3
		result[i-1].red4=atoi(CURRENT_RECORDS[i*CURRENT_COLS+4]);//red4
		result[i-1].red5=atoi(CURRENT_RECORDS[i*CURRENT_COLS+5]);//red5
		result[i-1].red6=atoi(CURRENT_RECORDS[i*CURRENT_COLS+6]);//red6
		result[i-1].blue=atoi(CURRENT_RECORDS[i*CURRENT_COLS+7]);//blue
	}
return result;
}
RECORD getDBFormer(RECORD data)
{
	RECORD result;
     char **CURRENT_RECORDS;//所有记录
     int CURRENT_ROWS;
     int CURRENT_COLS;
     sqlite3*  CURRENT_DB; 	
     char *term=data.term;
if(DB_DEBUG)printf("former term is %s\n",term);     
 char sql[256]="select trim(term)as term,red1,red2,red3,red4,red5,red6,blue FROM _DBHISTORY where term<'";
      strcat(sql,term);
       strcat(sql,"' order by term desc limit 1");
if(DB_DEBUG)printf("former term sql is %s\n",sql);            
 query(CURRENT_DB,sql,&CURRENT_RECORDS,&CURRENT_ROWS,&CURRENT_COLS);
  RECORD* result2 = rowsToRecords(CURRENT_RECORDS, CURRENT_ROWS, CURRENT_COLS);
result=result2[0];
if(DB_DEBUG)printf("former record is %s\n",formatRecord(result));
return result;
}
/*
get Next term deal cross year
*/
int getNextTerm(char *term)
{

int nextterm=0;
struct tm date,*ptm;
 time_t t;
time (&t);
 ptm=gmtime(&t);
int year=1900+ptm->tm_year;
char dest[5];
strncpy(dest, term,4);
dest[4]='\0';
int termyear=atoi(dest);

if( (year-termyear)==1)
 {
if(1)printf("term is |%s| termyear is %d currentYear is %d,year distance %d\n",term,termyear,year,year-termyear); 
 char n[8];
sprintf(n,"%d%s",year+1,"001");
n[8]='\0';
nextterm=atoi(n);
}else
{
 nextterm=atoi(term)+1;
}
return nextterm;
}
void copyRECORD(RECORD *src,RECORD *tgt)
{
 strcpy(tgt->desc,src->desc);
 strcpy(tgt->term,src->term);
tgt->red1=src->red1;
tgt->red2=src->red2;
tgt->red3=src->red3;
tgt->red4=src->red4;
tgt->red5=src->red5;
tgt->red6=src->red6;
tgt->blue=src->blue;
}
void printSTATICS(STATICS* indata,int size)
{
for(int t=0;t<size;t++)
{
  printf("\r%s%s",BLANK,formatRecord(indata[t].record));
  printf("\t\tpredication:%s\n",formatRecord(indata[t].predrecord));
  printf("%s%28c\t\t%12c%s\n",BLANK,' ',' ',formatRecord(indata[t].nextrecord));
	}
}
